%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% createMissingDirs.m 
% generateOutputDirectory.m 
% getFileSequenceFromRepresentative.m 
% readImageFile.m 
% readImagesByIndexRange.m 
% readdpx.m 
% setUpProjectDirectories.m 
% Image processing toolbox
%
%==========================================================================

%==========================================================================
% The function "setUpProjectWithMatlabReadableOrDpxImage" takes a string
% specifying a Matlab-readable image file and sets up a file structure with
% various information relevant to optical sound extraction from the given
% (type of) file.
% INPUT: "projectStruct" is a structure whose fields store various
% information relevant to optical sound extraction.
% OUTPUT: "projectStruct" returns a structure whose fields store various
% information relevant to optical sound extraction from the given type of
% files.
%==========================================================================
function projectStruct = setUpProjectWithMatlabReadableOrDpxImage(projectStruct)

% Get the file extension of the input file.
[~, ~, fileExt] = fileparts(projectStruct.inFile.name);
% Get rid of the dot before the file extension.
fileExt = fileExt(2:end);

% Try to get image information from the given file for the sake of checking
% if it is Matlab-readable.
if (strcmpi(fileExt, 'dpx') == false)
    imfinfo(projectStruct.inFile.name);
end

% Try to read the given file as an image file.
[imageArr, bitDepth] = readImageFile(projectStruct.inFile.name, fileExt);

%==========================================================================
% If this line is reached, we assume that the given file is either a .dpx
% image, or an image in format that is readable by Matlab.
%==========================================================================
% Classify the input file as a Matlab-readable or a .dpx image.
projectStruct.inFile.kind = 'imageMatlabOrDpx';

% Set the dimensions of each frame.
sizeArr = size(imageArr);
projectStruct.height = sizeArr(1);
projectStruct.width = sizeArr(2);
if (numel(sizeArr) == 2)
    projectStruct.numChannels = 1;
else
    projectStruct.numChannels = sizeArr(3);
end
% Set the bit depth per channel.
projectStruct.inFile.bitDepth = bitDepth;
projectStruct.workFile.bitDepth = bitDepth;

% Start a progress bar as the following may take a while.
waitbarHandle = waitbar(0.1, 'Reading frames...');
% Find the maximal run of image files with consecutive indices that match
% and include the specified template.
[projectStruct.inFile.dirPath, projectStruct.inFile.namePrefixNoPath, ...
    projectStruct.inFile.indFieldLength, projectStruct.inFile.fileExt, ...
    projectStruct.inFile.frameRangeArr] = ...
    getFileSequenceFromRepresentative(projectStruct.inFile.name);
% Close the progress bar.
close(waitbarHandle);
projectStruct.workFile.frameRangeArr = projectStruct.inFile.frameRangeArr;

% Set and create the output and scratch directories. Also set a project
% file name and file name prefix for converted image files if any are to be
% generated later on.
projectStruct = setUpProjectDirectories(projectStruct);

% Set a handle to the reading function.
projectStruct.readFramesFunctHandle = @(indRange, doConvertToGray) ...
    readImagesByIndexRange(indRange, [projectStruct.inFile.dirPath, ...
    projectStruct.inFile.namePrefixNoPath], projectStruct.inFile.indFieldLength, ...
    projectStruct.inFile.fileExt, doConvertToGray);

% Set a handle to a safer reading function (exactly the same function in
% the case at hand but different when the input is a video file).
projectStruct.readFramesSafelyFunctHandle = @(indRange, doConvertToGray) ...
    readImagesByIndexRange(indRange, [projectStruct.inFile.dirPath, ...
    projectStruct.inFile.namePrefixNoPath], projectStruct.inFile.indFieldLength, ...
    projectStruct.inFile.fileExt, doConvertToGray);

return;
% end of the function "setUpProjectWithMatlabReadableOrDpxImage"
